#!/usr/bin/env bash

# bl2_image_no_partition_table()
# {

# }

boot_bl2_image()
{
	if grep -Eq "^BR2_TARGET_PLATFORM_NXP_S32_S32G=y$" "${BR2_CONFIG}"; then
        dd if=${BINARIES_DIR}/bl2_w_dtb.s32 of=${BINARIES_DIR}/bl2_w_dtb_part1.s32 bs=512 count=1
        dd if=${BINARIES_DIR}/bl2_w_dtb.s32 of=${BINARIES_DIR}/bl2_w_dtb_part2.s32 bs=512 skip=1
		echo "bl2_w_dtb_part2.s32"
	elif grep -Eq "^BR2_TARGET_PLATFORM_NXP_S32_S32R=y$" "${BR2_CONFIG}"; then
		echo "bl2_w_dtb.s32"
    else
        echo "bl2_w_dtb.s32"
	fi
}

boot_fip_image()
{
	if grep -Eq "^BR2_TARGET_PLATFORM_NXP_S32_S32G=y$" "${BR2_CONFIG}"; then
		echo "fip.bin"
	elif grep -Eq "^BR2_TARGET_PLATFORM_NXP_S32_S32R=y$" "${BR2_CONFIG}"; then
		echo "fip.bin"
    else
        echo "fip.bin"
	fi
}

#
# dtb_list extracts the list of DTB files from BR2_LINUX_KERNEL_INTREE_DTS_NAME
# in ${BR_CONFIG}, then prints the corresponding list of file names for the
# genimage configuration file
#
boot_fat_dtb_list()
{
	local DTB_LIST

	DTB_LIST="$(sed -n 's/^BR2_LINUX_KERNEL_INTREE_DTS_NAME="\([\/a-z0-9 \-]*\)"$/\1/p' "${BR2_CONFIG}")"

	for dt in $DTB_LIST; do
		echo -n "\"$(basename "${dt}").dtb\", "
	done
}

#
# linux_image extracts the Linux image format from BR2_LINUX_KERNEL_UIMAGE in
# ${BR_CONFIG}, then prints the corresponding file name for the genimage
# configuration file
#
boot_fat_linux_image()
{
	if grep -Eq "^BR2_LINUX_KERNEL_UIMAGE=y$" "${BR2_CONFIG}"; then
		echo "\"uImage\", "
	elif grep -Eq "^BR2_LINUX_KERNEL_IMAGE=y$" "${BR2_CONFIG}"; then
		echo "\"Image\", "
	elif grep -Eq "^BR2_LINUX_KERNEL_IMAGEGZ=y$" "${BR2_CONFIG}"; then
		echo "\"Image.gz\", "
	else
		echo "\"zImage\", "
	fi
}

boot_fat_pfe_firmware()
{
	if grep -Eq "^BR2_PACKAGE_S32_PFENG=y$" "${BR2_CONFIG}"; then
		echo "\"firmware/s32g_pfe_class.fw\", "
	fi
}

genimage_type()
{
	if grep -Eq "^BR2_TARGET_PLATFORM_NXP_S32_S32G=y$" "${BR2_CONFIG}"; then
		echo "genimage.cfg.template"
	elif grep -Eq "^BR2_TARGET_PLATFORM_NXP_S32_S32R=y$" "${BR2_CONFIG}"; then
        echo "genimage.cfg.template_s32r"
    else
		echo "genimage.cfg.template"
	fi
}

main()
{
	local BL2_W_DTB_S32 FIP_BIN BOOT_FAT_FILES IMXOFFSET GENIMAGE_CFG GENIMAGE_TMP
    BL2_W_DTB_S32="$(boot_bl2_image)"
    FIP_BIN="$(boot_fip_image)"
	BOOT_FAT_FILES="$(boot_fat_dtb_list) $(boot_fat_linux_image) $(boot_fat_pfe_firmware)"
	GENIMAGE_CFG="$(mktemp --suffix genimage.cfg)"
	GENIMAGE_TMP="${BUILD_DIR}/genimage.tmp"

    echo "BL2_W_DTB_S32 is ${BL2_W_DTB_S32}"
    echo "FIP_BIN is ${FIP_BIN}"
    echo "BOOT_FAT_FILES is ${BOOT_FAT_FILES}"
    echo "BL2_W_DTB_S32 is ${BL2_W_DTB_S32}"

    echo "Debug information:"
    echo "GENIMAGE_CFG: ${GENIMAGE_CFG}"
    echo "Template file: board/alpha/s32cc/common/$(genimage_type)"
    echo "Generated config:"
    cat "${GENIMAGE_CFG}"

	sed -e "s|%BL2_W_DTB_S32%|${BL2_W_DTB_S32}|" \
        -e "s|%FIP_BIN%|${FIP_BIN}|" \
        -e "s|%BOOT_FAT_FILES%|${BOOT_FAT_FILES}|" \
		"board/alpha/s32cc/common/$(genimage_type)" > "${GENIMAGE_CFG}"

	rm -rf "${GENIMAGE_TMP}"

	genimage \
		--rootpath "${TARGET_DIR}" \
		--tmppath "${GENIMAGE_TMP}" \
		--inputpath "${BINARIES_DIR}" \
		--outputpath "${BINARIES_DIR}" \
		--config "${GENIMAGE_CFG}"

	rm -f "${GENIMAGE_CFG}"

	exit $?
}

main "$@"
